package cn.com.nr.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.service.GasStationinfoService;
import cn.com.nr.entity.NrConcreteEndangerEntity;
import cn.com.nr.entity.NrDiggingsEndangerEntity;
import cn.com.nr.entity.NrRecordsRe2Entity;
import cn.com.nr.service.NrConcreteEndangerService;
import cn.com.nr.service.NrDiggingsEndangerService;
import cn.com.nr.service.NrRecordsRe2Service;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;



/**
 * 自然资源采集记录-重跑2
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2025-05-15 17:35:56
 */
@Controller
@RequestMapping("/nr/records2")
public class NrRecordsRe2Controller {
	@Autowired
	private NrRecordsRe2Service nrRecordsRe2Service;
	@Autowired
	private HttpSessionService httpSessionService;
	
	@Autowired
	private NrDiggingsEndangerService nrDiggingsEndangerService;
	@Autowired
	private NrConcreteEndangerService nrConcreteEndangerService;
	@Autowired
	private GasStationinfoService gasStationinfoService;
	
	@Autowired
	private DeptService deptService;
	
	@ApiOperation(value = "删除")
	@DeleteMapping("/delete")
	@ResponseBody
	public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
		nrRecordsRe2Service.removeByIds(ids);
		return DataResult.success();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list")
	@ResponseBody
	public DataResult listhandle(@RequestBody NrRecordsRe2Entity nrRecords) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		String currentDeptId = httpSessionService.getCurrentDeptId();
		String deptId = nrRecords.getDeptId();
		String stationName = nrRecords.getStationName();
		String industry = nrRecords.getIndustry();
		List<String> stationinfoIdList = new ArrayList<>();
		
		// 矿山
		LambdaQueryWrapper<NrDiggingsEndangerEntity> queryWrapperNrDiggingsEndanger = Wrappers.lambdaQuery();
		if(TsBqUtils.isNotNull(deptId)){
			queryWrapperNrDiggingsEndanger.eq(NrDiggingsEndangerEntity::getTaxorgcode, deptId);
		}else{
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapperNrDiggingsEndanger.eq(NrDiggingsEndangerEntity::getTaxorgcode, currentDeptId);

			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapperNrDiggingsEndanger.like(NrDiggingsEndangerEntity::getTaxorgcode, currentDeptId.substring(0, 5));
			} else {
				// 省局
			}
		}
			
		if(TsBqUtils.isNotNull(stationName)){
			queryWrapperNrDiggingsEndanger.like(NrDiggingsEndangerEntity::getName, stationName);
		
		}
		
		if(TsBqUtils.isNotNull(industry)){
			queryWrapperNrDiggingsEndanger.eq(NrDiggingsEndangerEntity::getIndustry, industry);

		}
		List<NrDiggingsEndangerEntity> nrDiggingsList = nrDiggingsEndangerService.list(queryWrapperNrDiggingsEndanger);
		for (NrDiggingsEndangerEntity nrDiggingsEndangerEntity : nrDiggingsList) {
			String id = nrDiggingsEndangerEntity.getId();
			stationinfoIdList.add(id);
		}
		
		// 水泥和混凝土
		LambdaQueryWrapper<NrConcreteEndangerEntity> queryWrapperNrConcrete = Wrappers.lambdaQuery();
		
		if(TsBqUtils.isNotNull(deptId)){
			queryWrapperNrConcrete.eq(NrConcreteEndangerEntity::getTaxorgcode, deptId);
		}else{
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapperNrConcrete.eq(NrConcreteEndangerEntity::getTaxorgcode, currentDeptId);

			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				queryWrapperNrConcrete.like(NrConcreteEndangerEntity::getTaxorgcode, currentDeptId.substring(0, 5));
			} else {
				// 省局
			}
		}
		
		if(TsBqUtils.isNotNull(stationName)){
			queryWrapperNrConcrete.like(NrConcreteEndangerEntity::getName, stationName);
		}
		
		if(TsBqUtils.isNotNull(industry)){
			queryWrapperNrConcrete.eq(NrConcreteEndangerEntity::getIndustry, industry);

		}
		List<NrConcreteEndangerEntity> nrConcreteList = nrConcreteEndangerService.list(queryWrapperNrConcrete);
		for (NrConcreteEndangerEntity nrConcreteEndangerEntity : nrConcreteList) {
			String id = nrConcreteEndangerEntity.getId();
			stationinfoIdList.add(id);
		}
		
		// 查询企业
		if (TsBqUtils.isNotNull(stationinfoIdList)) {

			if (TsBqUtils.isNull(nrRecords.getCreateTimeStart())) {
				return DataResult.success("start-MessageDisappear不能为空！"); 
			}
			if (TsBqUtils.isNull(nrRecords.getCreateTimeEnd())) {
				return DataResult.success("end-MessageDisappear不能为空！");
			}
			
			Page page = new Page(nrRecords.getPage(), nrRecords.getLimit());
			LambdaQueryWrapper<NrRecordsRe2Entity> queryWrapper = Wrappers.lambdaQuery();
			// 查询条件示例
			if (!StringUtils.isEmpty(nrRecords.getCreateTimeStart())) {
				queryWrapper.ge(NrRecordsRe2Entity::getTime,
						sdf.parse(nrRecords.getCreateTimeStart() + " 00:00:00"));
			}
			
			if (!StringUtils.isEmpty(nrRecords.getCreateTimeEnd())) {
				queryWrapper.le(NrRecordsRe2Entity::getTime,
						sdf.parse(nrRecords.getCreateTimeEnd() + " 23:59:59"));
			}
			
			queryWrapper.in(NrRecordsRe2Entity::getNrEndanger, stationinfoIdList);
			queryWrapper.orderByDesc(NrRecordsRe2Entity::getTime);
			
			IPage<NrRecordsRe2Entity> _iPage = nrRecordsRe2Service.page(page, queryWrapper);
			
			List<NrRecordsRe2Entity> records = _iPage.getRecords();
			
			for (NrRecordsRe2Entity _nrRecords : records) {
				String nrEndanger = _nrRecords.getNrEndanger();
				GasStationinfoEntity gasStationinfoEntity = gasStationinfoService.getById(nrEndanger);

				_nrRecords.setStation(TsBqUtils.handleEndBeforeParentheses(gasStationinfoEntity.getName()));
				String taxorgcode = gasStationinfoEntity.getTaxorgcode();

				if (TsBqUtils.isNotNull(taxorgcode)) {
					String taxorgname = deptService.getById(taxorgcode).getName();
					_nrRecords.setDeptName(taxorgname);
				}
			}
			return DataResult.success(_iPage);

		} else {
			return DataResult.fail("暂无数据");
		}

	}
	
}
